//
//  HorizonalTableViewController.m
//  oschina_song
//
//  Created by song on 16/1/25.
//  Copyright © 2016年 song. All rights reserved.
//

#import "HorizonalTableViewController.h"


static NSString *kHorizonalCellID =@"HorizonalCell";

@implementation HorizonalTableViewController

-(id)initWithViewControllers:(NSArray *)controllers
{
    self = [super init];
    if(self){
        
        _controllers = [NSMutableArray arrayWithArray:controllers];
        for(UIViewController *vc  in controllers)
        {
            [self addChildViewController:vc];
        }
    }
    
    return self;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _controllers.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return tableView.frame.size.height;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell =[self.tableView dequeueReusableCellWithIdentifier:kHorizonalCellID forIndexPath:indexPath];
    UIViewController *controller = _controllers[indexPath.row];
    controller.view.frame  = cell.contentView.bounds;
    [cell.contentView addSubview:controller.view];
    
    return cell;
}

-(void)viewDidLoad
{
     [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kHorizonalCellID];
}


-(void)scrollToViewAtIndex:(NSUInteger)index
{
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:UITableViewScrollPositionNone animated:NO];
}


@end
